package com.beyond.trace.service.impl.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beyond.trace.domain.TraceBase;
import com.beyond.trace.domain.storage.Order;
import com.beyond.trace.domain.storage.OrderDetail;
import com.beyond.trace.domain.storage.OrderVo;
import com.beyond.trace.mapper.TraceBaseMapper;
import com.beyond.trace.mapper.storage.OrderMapper;
import com.beyond.trace.requestVo.OrderQuery;
import com.beyond.trace.requestVo.StoreVo;
import com.beyond.trace.service.ITraceBaseService;
import com.beyond.trace.service.storage.IOrderDetailService;
import com.beyond.trace.service.storage.IOrderService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 订单主表信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService
{
    @Resource
    private OrderMapper orderMapper;

    @Autowired
    private IOrderDetailService orderDetailService;

    @Override
    public List<Order> getOrderPage(OrderQuery orderQuery) {
        if(StringUtils.isNotEmpty(orderQuery.getStartTime())) {
            orderQuery.setStartTime(orderQuery.getStartTime()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(orderQuery.getEndTime())) {
            orderQuery.setEndTime(orderQuery.getEndTime()+" 23:59:59");
        }
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            orderQuery.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        }

        return orderMapper.getOrderPage(orderQuery);
    }

    @Override
    public OrderVo getOrderById(String id) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            return orderMapper.getOrderById(id,String.valueOf(SecurityUtils.getUserDepartId()));
        }
        return orderMapper.getOrderById(id,"");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addOrder(OrderVo orderVo) {
        try {
            Order order = new Order();
            BeanUtils.copyProperties(orderVo,order);
            String id = SnowUtil.getId();
            order.setId(id);
            order.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
            if(StringUtils.isEmpty(order.getOrderNo())) {
                order.setOrderNo(SnowUtil.getId());
            }
            BigDecimal totalCount = BigDecimal.ZERO;
            BigDecimal totalMoney = BigDecimal.ZERO;
            if(StringUtils.isNotEmpty(orderVo.getList())) {
                List<OrderDetail> orderDetailList = orderVo.getList();
                for (OrderDetail s : orderDetailList) {
                    s.setId("");
                    s.setOrderId(id);
                    totalCount = totalCount.add(s.getCount());
                    BigDecimal multiply = s.getPrice().multiply(s.getCount());
                    s.setTotal(multiply);
                    totalMoney = totalMoney.add(multiply);
                    s.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
                    s.setCreateBy(SecurityUtils.getUsername());
                    s.setUpdateBy(SecurityUtils.getUsername());
                    s.setCreateTime(new Date());
                    s.setUpdateTime(new Date());
                }
                orderDetailService.saveBatch(orderVo.getList());
            }
            order.setTotalCount(totalCount);
            order.setTotalMoney(totalMoney);
            order.setCreateBy(SecurityUtils.getUsername());
            order.setUpdateBy(SecurityUtils.getUsername());
            order.setCreateTime(new Date());
            order.setUpdateTime(new Date());
            this.save(order);
            return 1;
        }catch (Exception e) {
            log.error("添加订单信息异常："+e.getMessage());
            return 0;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editOrder(OrderVo orderVo) {

        try {
            Order order = new Order();
            BeanUtils.copyProperties(orderVo,order);
            Map<String,Object> removeMap = new HashMap<>();
            removeMap.put("order_id",order.getId());
            orderDetailService.removeByMap(removeMap);
            BigDecimal totalCount = BigDecimal.ZERO;
            BigDecimal totalMoney = BigDecimal.ZERO;
            if(StringUtils.isNotEmpty(orderVo.getList())) {
                List<OrderDetail> orderDetailList = orderVo.getList();
                for (OrderDetail s : orderDetailList) {
                    s.setId("");
                    s.setOrderId(order.getId());
                    totalCount = totalCount.add(s.getCount());
                    BigDecimal multiply = s.getPrice().multiply(s.getCount());
                    s.setTotal(multiply);
                    totalMoney = totalMoney.add(multiply);
                    s.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
                    s.setCreateBy(SecurityUtils.getUsername());
                    s.setUpdateBy(SecurityUtils.getUsername());
                    s.setCreateTime(new Date());
                    s.setUpdateTime(new Date());
                }
                orderDetailService.saveBatch(orderVo.getList());
            }
            order.setTotalCount(totalCount);
            order.setTotalMoney(totalMoney);
            order.setUpdateBy(SecurityUtils.getUsername());
            order.setUpdateTime(new Date());
            this.updateById(order);
            return 1;
        }catch (Exception e) {
            log.error("修改订单信息异常："+e.getMessage());
            return 0;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteOrderById(String id) {
        try {
            List<String> list = Arrays.asList(id.split(","));
            this.removeByIds(list);
            Map<String,Object> removeMap = new HashMap<>();
            removeMap.put("order_id",id);
            QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("order_id",list);
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
                removeMap.put("depart_id",String.valueOf(SecurityUtils.getUserDepartId()));
                queryWrapper.eq("depart_id",String.valueOf(SecurityUtils.getUserDepartId()));
            }
            orderMapper.deleteByMap(removeMap);
            orderDetailService.remove(queryWrapper);
            return 1;
        }catch (Exception e) {
            log.error("修改订单信息异常："+e.getMessage());
            return 0;
        }
    }

    @Override
    public List<StoreVo> getStorePage(String productName) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            return orderMapper.getStorePage(productName,String.valueOf(SecurityUtils.getUserDepartId()));
        }
        return orderMapper.getStorePage(productName,"");
    }
}
